package com.gonfre.quientelo;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;

import util.AsyncRequest;
import util.FilterValues;
import util.RequestListAdapter;
import util.Response;
import util.Tools;


public class RequestListActivity extends MyMenuActivity {

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    private boolean valuesFilterBarInitialized = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_request_list);

        mRecyclerView = (RecyclerView) findViewById(R.id.itemsRecyclerView);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mSwipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipeRefreshLayout);
        mSwipeRefreshLayout.setColorScheme(R.color.titleBar);
        mSwipeRefreshLayout.setSoundEffectsEnabled(true);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadData();
            }
        });

        filterBar = (Toolbar)findViewById(R.id.secondBarFilter);
        sorterBar = (Toolbar)findViewById(R.id.secondBarSort);
        filterBar.setVisibility(View.GONE);
        sorterBar.setVisibility(View.GONE);
        initializeSorterBar(R.id.secondBarSort, SortFragment.SELLER);
        initializeFilterBar(R.id.secondBarFilter, FiltersFragment.SELLER);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_request_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_filter) {
            if (Tools.isVisibleSecondBar(sorterBar)) {
                Tools.toggleSecondBar(sorterBar);
            }
            Tools.toggleSecondBar(filterBar);

            return true;
        } else if (id == R.id.action_sort) {
            if (Tools.isVisibleSecondBar(filterBar)) {
                Tools.toggleSecondBar(filterBar);
            }
            Tools.toggleSecondBar(sorterBar);

            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onFinishRequest(int type, Response res) {
        stopProgressBarCircular(R.id.progressBarRequestList);
        mSwipeRefreshLayout.setRefreshing(false);

        if(res.getHeaderResponse() == 0){
            mAdapter = new RequestListAdapter(res);
            mRecyclerView.setAdapter(mAdapter);

            if (!valuesFilterBarInitialized) {
                initValuesFilterBar(res);
                //init second bars
                Tools.initSecondBar(filterBar, (RelativeLayout)findViewById(R.id.MainLayout), toolbar);
                Tools.initSecondBar(sorterBar, (RelativeLayout)findViewById(R.id.MainLayout), toolbar);
            }
        } else {
            //CONTROLAR ERROR
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        //load data
        loadData();
    }

    public void loadData() {
        startProgressBarCircular(R.id.progressBarRequestList);

        this.setKey("f10e6b253f8c4e8a52b1244a1d7a5edc");

        AsyncRequest req = new AsyncRequest(this);
        Response res = new Response();

        res = req.xGetRequestList("*", "*", "*");
    }

    protected void initValuesFilterBar(Response res) {

        valuesFilterBarInitialized = true;

        FilterValues filtros = Tools.createFilterValuesFromResponse(res, FiltersFragment.SELLER);

        FiltersFragment filtersFragment = (FiltersFragment)getFragmentManager().findFragmentById(R.id.secondBarFilter);
        filtersFragment.setInitialValues(filtros);
    }
}
